#ifndef _PIECE_H_
#define _PIECE_H_

#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "sha1.h"
#include "net.h"
#include "storage.h"

#define BLOCK_SIZE 16384

class Piece
{
public:
	// Piece Data
	unsigned GetPieceID();
	unsigned GetBlockSize(unsigned block_num=0);
	unsigned GetNumBlocks();
	void SetData(unsigned char *data, unsigned len);

private:
	unsigned m_pieceid;
	unsigned m_piecesize;
	unsigned m_blocksize;
	unsigned m_numblocks;

	unsigned char *m_data;
	unsigned char *m_bitfield;

	// Pieces are only usable by Peer class
	friend class Peer;
	friend class Torrent;
	Piece(unsigned piece_id, unsigned piece_size);
	~Piece();
	bool CompareHashValue(unsigned char *hash_value);
	bool StoreBlock(unsigned block_num, unsigned char *data);
	bool WriteToStorage(Storage *storage);
};

#endif
